﻿using System;
using System.Data;
using System.Windows.Forms;
using Login.BLL;

namespace Courses
{
    public partial class tb_shangkeClass : Form
    {
        public tb_shangkeClass()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void tb_shangkeClass_Load(object sender, EventArgs e)
        {
            //提取班级表格方法
            Bind();
        }
        #endregion

        /// <summary>
        /// 提取班级表格方法
        /// </summary>
        #region
        public void Bind()
        {
            //提取班级表
            DataSet ds = tb_shangkeClassBLL.getData();
            //添加到窗体
            biao.DataSource = ds.Tables[0];
        }
        #endregion

        /// <summary>
        /// 添加班级时间事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void 添加_Click(object sender, EventArgs e)
        {
            Success.Visible = false;
            tiShi.Visible = false;
            //将这个时间所有上课班级的实验数赋给classNum
            int classNum = banJi.Items.Count;
            //判断时间和班级是否有为空的
            if (classNum == 0 || Time.Text == "")
            {
                tiShi.Visible = true;
                return;
            }
            //循环遍历所有选中班级
            for (int i = 0; i < classNum; i++)
            {
                string kai = banJi.Items[i].Text;
                string yang = Time.Text.Trim();
                //添加班级上课时间
                tb_shangkeClassBLL.timeAdd(banJi.Items[i].Text,Time.Text.Trim());
            }
            Success.Visible = true;
            banJi.Items.Clear();
            //提取班级表格方法
            Bind();
        }
        #endregion

        /// <summary>
        /// 表格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void biao_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Success.Visible = false;
            //将这个时间所有上课班级的实验数赋给classNum
            int classNum = banJi.Items.Count;
            //循环遍历是否重复选中
            for (int i = 0; i < classNum; i++)
            {
                if (banJi.Items[i].Text==biao.SelectedRows[0].Cells[0].Value.ToString())
                {
                    return;
                }
            }
            //判断是否选中了已安排实验的班级
            if (biao.SelectedRows[0].Cells[1].Value.ToString() =="已安排")
            {
                //判断是否修改已安排班级的上课时间
                if (MessageBox.Show("所选班级已安排相应时间上课，是否修改该班级上课时间？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    banJi.Items.Add(biao.SelectedRows[0].Cells[0].Value.ToString());
                    //将选中的已安排上课的班级上课时间修改
                    tb_shangkeClassBLL.timeModify(biao.SelectedRows[0].Cells[0].Value.ToString());
                    //刷新表格
                    Bind();
                    return;
                }
                else
                {
                    return;
                }
            }
            banJi.Items.Add(biao.SelectedRows[0].Cells[0].Value.ToString());
        }
        #endregion

        /// <summary>
        /// 所选班级清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void btn1_Click(object sender, EventArgs e)
        {
            Success.Visible = false;
            banJi.Items.Clear();
        }
        #endregion

        /// <summary>
        /// 窗体点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        #region
        private void tb_shangkeClass_Click(object sender, EventArgs e)
        {
            Success.Visible = false;
        }
        #endregion
    }
}
